﻿using System;
using System.Collections.Generic;
using System.Text;
using net.veekee.common.data;
using net.veekee.common;
using net.veekee.logprocessor.plugin.context;

namespace net.veekee.logprocessor.plugin.input
{
    public class FileEntry : AnEntry
    {
        public static FileEntry CurrentEntry = null;

        private List<long> outputfiles = null;
        public List<long> OutputFiles
        {
            get
            {
                return this.outputfiles;
            }
        }

        private AnEntryType type = AnEntryType.Regular;
        public AnEntryType Type
        {
            get
            {
                return this.type;
            }
            set
            {
                this.type = value;
            }
        }

        private bool validated = false;
        public bool Validated
        {
            get
            {
                return this.validated;
            }
        }

        public FileEntry()
        {
            this.outputfiles = new List<long>();
        }

        public void internalValidate(params String[] args)
        {
            if (args.Length == 0)
                args = ProcessorContext.GetInstance().AvailableOutputs;
            Log.GetInstance().Trace("Validating entry for {0}", String.Join(", ", args));
            this.outputfiles.Clear();

            FileContext outputfile = null;
            for (int i = 0; i < args.Length; i++)
            {
                outputfile = ProcessorContext.GetInstance().GetOutput(args[i]);
                if (outputfile != null)
                {
                    this.outputfiles.Add(outputfile.Context.ID);
                }
            }
            if (this.outputfiles.Count > 0)
            {
                if (!FileEntry.CurrentEntry.Equals(this))
                {
                    Log.GetInstance().Trace("Subvalidation has been detected");
                }
                ValidationPool.GetInstance().Entries.Add(this);
                this.validated = true;
            }
            else
            {
                Log.GetInstance().Error("Validation is not effective (no output file)");
            }
        }

        public void internalValidate()
        {
            this.internalValidate(ProcessorContext.GetInstance().AvailableOutputs);
        }

        public void Validate(params String[] args)
        {
            this.internalValidate(args);
        }

        public void Validate()
        {
            this.internalValidate(ProcessorContext.GetInstance().AvailableOutputs);
        }
    }

    public enum AnEntryType
    {
        Regular,
        Special
    }
}
